/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package projecteuler;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ChunchO
 */
public class Problem034 {
    public static List<Integer> numeros = new ArrayList<Integer>();
    public static void main(String[] args){
        for(int i =10;i<=10000000;i++)
        {
            int suma = 0;
            String s = Integer.toString(i);
            for(int j = 0;j<s.length();j++)
                suma+=factorial(s.charAt(j)-'0');
            if(suma==i)
                numeros.add(i);
        }
        int total = 0;
        for(int i = 0;i<numeros.size();i++)
            total+=numeros.get(i);
        System.out.println(total);
    }
    public static int factorial(int n)
    {
        if(n == 0)
            return 1;
        int mult = 1;
        for(int i = 1;i<=n;i++)
            mult *=i;
        return mult;
    }
}
